package com.ee.chat.cps.admin.controller.login;

import com.ee.chat.common.bean.Result;
import com.ee.chat.cps.admin.service.ILoginService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
public class LoginController {
    private Logger log = LoggerFactory.getLogger(LoginController.class);

    @Resource
    ILoginService loginService;

    /**
     * 登录
     * @Author Shane
     * @Date 2019/7/1116:21
     * @param
     * @return
     **/
    @RequestMapping(value = "login", method = RequestMethod.POST)
    public Result login(HttpServletRequest request, @RequestParam String username, @RequestParam String password){
        try {
            return loginService.login(request, username, password);
        } catch (Exception e){
            log.error("【登录错误】", e);
        }
        return Result.returnFailure();
    }

    /**
     * 注销
     * @Author Shane
     * @Date 2019/7/1116:22
     * @param
     * @return
     **/
    @RequestMapping("logout")
    public Result logout(HttpServletRequest request){
        HttpSession session = request.getSession(true);
        session.removeAttribute("channelUser");
        return Result.returnSuccess(null);
    }

    /**
     * 修改密码
     * @Author Shane
     * @Date 2019/9/1020:35
     * @param
     * @return
     **/
    @RequestMapping("updPwd")
    public Result updPwd(HttpServletRequest request, @RequestParam String oldPwd, @RequestParam String newPwd){
        try {
            return loginService.updPwd(request, oldPwd, newPwd);
        } catch (Exception e){
            log.error("【修改密码错误】", e);
        }
        return Result.returnFailure();
    }
}
